<?

require_once ("{$_APP_SESSION_USER->theme}/template.calendar.inc");

global $_OBJECT_IFACE;
global $_OBJECT_IFACE_PANEL;

$_OBJECT_IFACE_PANEL->insert_container (display_calendar (array (
		cal_option=>"mo_view",
		no_add_entry=>true,
		link_options=>"fs_tab=calendar&timesheet_id=&fs_action=view&",
		timestamp=>isset ($_REQUEST["timestamp"]) ? $_REQUEST["timestamp"] : time (),
		callback=>"_app_get_calendar_dates",
	)));

function _app_get_calendar_dates ($day, $week, $month, $calendar) {

	global $_GLOBAL_DBH;
	print "<br>Day Schedule<br><br>";

	$times = $_GLOBAL_DBH->query ("SELECT id,date_begin,CASE date_end WHEN '' THEN CURRENT_TIMESTAMP() ELSE date_end END AS date_end,UNIX_TIMESTAMP(date_begin) AS begin_utime,UNIX_TIMESTAMP(CASE date_end WHEN '' THEN CURRENT_TIMESTAMP() ELSE date_end END) AS end_utime FROM timesheet WHERE (UNIX_TIMESTAMP(CASE date_end WHEN '' THEN CURRENT_TIMESTAMP() ELSE date_end END) >= {$day->begin} AND UNIX_TIMESTAMP(date_begin) <= {$day->end}) OR (UNIX_TIMESTAMP(date_end) >= {$day->begin} AND UNIX_TIMESTAMP(date_end) <= {$day->end})");  
	$utimes = array();
	while ($utime = $_GLOBAL_DBH->fetch_object ($times)) {

		$begin = null;
		$end = null;

		if ($utime->begin_utime >= $day->begin) {
			if (!$begin) $begin = $utime->begin_utime;
			print "Begin :: <b>{$utime->date_begin}</b><br>";
		}
		if ($utime->end_utime <= $day->end) {
			$end = $utime->end_utime;
			print "End :: <b>{$utime->date_end}</b><br>";
		}

		if (!$begin) $begin = $day->begin;
		if (!$end) $end = $day->end;
		$utimes[] = "($end-$begin)";
	}

	$utimes_total = join ($utimes, "+");

	$total = $_GLOBAL_DBH->query ("SELECT SEC_TO_TIME($utimes_total) AS total_time");
	$total = $_GLOBAL_DBH->fetch_object ($total);
	print "<br>Total Time :: <b>{$total->total_time}</b><br><br>";

}

?>
